package pathways.util;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.awt.Point;
import java.awt.Rectangle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Vector;

import pathways.CCoOrdinates;
import pathways.CCompound;
import pathways.CEnzyme;
import pathways.CPathway;
import pathways.CReaction;
import pathways.CSubstrate;
import pathways.util.Debug.CDebug;

public class CUtil
{
  public static int getResultSetRowCount(ResultSet rs) throws SQLException
  {
    if (!CUtil.isEmptyRS(rs))
    {
      // exception only if database access error (like server reboot in the day)
      int rowBackup = rs.getRow();
      
      rs.last();
      
      int lastRow = rs.getRow();
      
      if (rowBackup == 0)
         rs.beforeFirst();
      else
        rs.absolute(rowBackup);
      
      return lastRow;
    }
    else
    {
      return 0;
    }
  }
  
  public static int getRowInRS(ResultSet rs, int columnIndex, String value) throws SQLException
  {
    int rowBackup = rs.getRow();
    boolean found = false;
    
    rs.beforeFirst();
    String valueInRS = null;
    
    while(rs.next() && !found)
    {   
       valueInRS = rs.getString(columnIndex);
          
       if (valueInRS.equals(value))
       {
         found = true;
       }
    }
    
    int row = rs.getRow();
    
    rs.absolute(rowBackup);
    
    return row;
  }
  
  public static boolean isEmptyRS(ResultSet rs)
  {
    boolean isEmpty = false;
    
    try{
      isEmpty = (rs.isBeforeFirst() && rs.isAfterLast());
    } catch(SQLException ex)
    {
      CDebug.println("pathways.util.CUtil",ex.toString()); 
    }
    
    return isEmpty;
  }
  
  public static Point getSmallestXandYOfSubs(CPathway pathway)
  {
    Vector substrates = pathway.getSubstrateList();
    CSubstrate sub;
    CCoOrdinates coords;
    Point topMostPoint;
    Point leftMostPoint;
    int smallestX = 0;
    int smallestY = 0;
    
    // initialize smallestX and smallestY to first substrate
    if (substrates.size() > 0)
    {
      sub = (CSubstrate)((LinkedList)substrates.get(0)).getFirst();
      
      smallestX = sub.getCoOrdinates().getPoint0().x;
      smallestY = sub.getCoOrdinates().getPoint1().y;
    }
    
    for (int i=0; i<substrates.size(); i++)
    {
      sub = (CSubstrate)((LinkedList)substrates.get(i)).getFirst();
      
      if (sub.isVisible())
      {
        coords = sub.getCoOrdinates();
        leftMostPoint = coords.getPoint0();
        topMostPoint = coords.getPoint1();
        
        if (leftMostPoint.x < smallestX)
          smallestX = leftMostPoint.x;
          
        if (topMostPoint.y < smallestY)
          smallestY = topMostPoint.y;
      }
    }
    
    return new Point(smallestX, smallestY);  
  }
  
  public static int getConnectingTo(int connectingFrom)
  {
    if (connectingFrom == 0)
       return 2;
    else if (connectingFrom == 1)
       return 3;
    else if (connectingFrom == 2)
       return 0;
    else if (connectingFrom == 3)
       return 1;
       
    return -1;
  }
  
  public static boolean isBindingProteinReaction(CReaction rxn)
  {
    Vector vSubs = rxn.getAllSubstrates();
    Vector vProds = rxn.getAllProducts();
    CEnzyme enz = rxn.getEnzyme();
    CCompound c = null;
    
    for (int i=0; i<vSubs.size(); i++)
    {
      c = (CCompound)vSubs.get(i);
      
      if (c.getID() == enz.getID())
         return true;
    } 

    for (int i=0; i<vProds.size(); i++)
    {
      c = (CCompound)vProds.get(i);
      
      if (c.getID() == enz.getID())
         return true;
    } 
    
    return false;
  }
  
  public static boolean isMissingInfoReaction(CReaction rxn)
  {
    return (rxn.getEnzyme().getID() > 0 ? false : true); 
  }
  
  public static String getString(Rectangle rect)
  {
    String s = "";
    
    s = rect.x + "," + rect.y + "," + rect.width + "," + rect.height;
    
    return s; 
  }
  
  public static Rectangle getRect(String rect)
  {
    // rect = x,y,width,height
    
    int posX = rect.indexOf(",",0);
    String x_str = rect.substring(0,posX);
    
    int posY = rect.indexOf(",",posX+1);
    String y_str = rect.substring(posX+1,posY);
    
    int posWidth = rect.indexOf(",",posY+1);
    String width_str = rect.substring(posY+1,posWidth);
    
    String height_str = rect.substring(posWidth+1);
    
    int x = Integer.parseInt(x_str);
    int y = Integer.parseInt(y_str);
    int width = Integer.parseInt(width_str);
    int height = Integer.parseInt(height_str);
    
    return (new Rectangle(x,y,width,height));
  }
  
  public static String getCurrentTime()
  {
    // returns current time in the format MM/dd/yy HH:mm:ss
    // example: 02/23/02 19:00:00
    
    java.util.Date d = new java.util.Date();
    java.text.SimpleDateFormat s = new java.text.SimpleDateFormat("MM/dd/yy HH:mm:ss");
    
    return s.format(d);  
  }
  
  public static String formatECNO(String ecno)
  {
    String strECNO = "";
    
    if (ecno.length() < 8)
       return ecno;
    
    try {
      strECNO = "" + Integer.parseInt(ecno.substring(0,1)); 
      strECNO = strECNO + "." + Integer.parseInt(ecno.substring(1,3));
      strECNO = strECNO + "." + Integer.parseInt(ecno.substring(3,5));
      strECNO = strECNO + "." + Integer.parseInt(ecno.substring(5,8));
    } catch (Exception e) { 
      return ecno; 
    }
    
    return strECNO;  
  }
  
}